/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tpgen.grammar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
 *
 * @author shanki
 */
public class Variables {
    private Map<Symbol, ArrayList<Variable>> variables  = new HashMap<Symbol, ArrayList<Variable>>();
    private ArrayList<Variable> vs                      = new ArrayList<Variable>();

    public Variables() {
    }
    
    public Variables(Set<Variable> vars) {
        for (Variable var : vars) {
            add(var);
        }
    }
    
    public final void add(Variable variable) {
        
        Symbol type = variable.getType();
        
        ArrayList<Variable> vars    = variables.get(type);
        
        if (vars == null) {
            vars    = new ArrayList<Variable>();
            variables.put(type, vars);
        }
        
        vars.add(variable);
        vs.add(variable);
    }

    int size(Symbol type) {
        ArrayList<Variable> vars = variables.get(type);
        
        if (vars == null) {
            return 0;
        } else {
            return vars.size();
        }
    }
    
    int size() {
        return vs.size();
    }
    
    public boolean isEmpty() {
        return size() == 0;
    }

    Variable get(Symbol type, int index) {
        return variables.get(type).get(index);
    }
    
    Variable get(int index) {
        return vs.get(index);
    }

    public Variable random(Random rg) {
        int index = rg.nextInt(size());        
        return vs.get(index);
    }

    public Variables remove(Variable a) {
        Variables vvv = new Variables();

        for (Variable var : vs) {
            if (var != a) {
                vvv.add(var);
            }
        }

        return vvv;
    }
}
